clear

/// Main variables: 
/// treatment: treatment = 1 is AON, treatment = 2 is sGMS, treatment = 3 is dGMS1
/// session: session number (1 to 6 in each treatment)
/// period: game round (1 to 45)
/// third: third = 1 (periods 1 to 15), third = 2 (periods 16 to 30), third = 1 (periods 31 to 45)
/// idnr: participant identifier
/// ppnr: participant identifier within group
/// valuation: value
/// decision: bid
/// earnings: value - payment if good is obtained
/// costs: costs of good
/// profitobjective: profitobjective=1 if costs are 50, 70 or 90; profitobjective=0 if costs are 60, 80 or 100
/// threshold: cutoff deciding on whether good is produced
/// reservationprice: minimum price
/// success: binary indicator whether good is produced
/// profit: producer profit
/// surplus: generated surplus
/// obtained: binary indicator whether good is obtained 
/// payment: paid price by participant
/// payoff: monetary payment to participant
/// age: participant age
/// female: self-reported gender
/// subject: study subject
/// experience: experience with crowdfunding campaigns

//Procedure for replicating results:
//1. Save the following 18 files in the same folder: AON1.xlsx, AON2.xlsx, AON3.xlsx, AON4.xlsx, AON5.xlsx, AON6.xlsx, sGMS1.xlsx, sGMS2.xlsx, sGMS3.xlsx, sGMS4.xlsx, sGMS5.xlsx, sGMS6.xlsx, dGMS1.xlsx, dGMS2.xlsx, dGMS3.xlsx, dGMS4.xlsx, dGMS5.xlsx, dGMS6.xlsx
//2. Define local path to the data files (e.g. local path "C:\Users\AndrejWoerner\CrowdfundingProject\Data")
local path ""
tempfile all_data
//3. Execute code

quietly {


forvalues i = 1/6 {
    local file = "dGMS`i'.xlsx"
    local sheet = "dGMS`i'"
    local fullpath = "`path'`file'"

    if `i' <= 3 {
        import excel "`fullpath'", sheet("`sheet'") cellrange(A187:R862) firstrow clear
    }
    else if `i' == 4 {
        import excel "`fullpath'", sheet("`sheet'") cellrange(A194:R869) firstrow clear
    }
    else if `i' == 5 {
        import excel "`fullpath'", sheet("`sheet'") cellrange(A195:R870) firstrow clear
    }
    else if `i' == 6 {
        import excel "`fullpath'", sheet("`sheet'") cellrange(A188:R863) firstrow clear
    }

    gen source = "`sheet'"

    if `i' == 1 {
        save "`all_data'"
    }
    else {
        append using "`all_data'"
        save "`all_data'", replace
    }
}
replace treatment="3" // dGMS is treatment 3

save "`path'all_dGMS_combined.dta", replace


clear
local sgmsvars ppnr period decision decisionother earnings time valuation submitted treatment session zeros
tempfile all_sgms

forvalues i = 1/6 {
    local file = "sGMS`i'.xlsx"
    local sheet = "sGMS`i'"
    local fullpath = "`path'`file'"

    if inlist(`i', 4, 6) {
        import excel "`fullpath'", sheet("`sheet'") cellrange(A142:K816) clear
    }
    else {
        import excel "`fullpath'", sheet("`sheet'") cellrange(A139:K813) clear
    }

    rename (A B C D E F G H I J K) (`sgmsvars')
    gen source = "`sheet'"

    if `i' == 1 {
        save "`all_sgms'"
    }
    else {
        append using "`all_sgms'"
        save "`all_sgms'", replace
    }
}

use "`path'all_dGMS_combined.dta", clear
append using "`all_sgms'"
save "`path'full_dGMS_sGMS_combined.dta", replace



clear
local aonvars ppnr period decision decisionother earnings time valuation submitted treatment session zeros
tempfile all_aon

forvalues i = 1/6 {
    local file = "AON`i'.xlsx"
    local sheet = "AON`i'"
    local fullpath = "`path'`file'"

    import excel "`fullpath'", sheet("`sheet'") cellrange(A139:K813) clear

    rename (A B C D E F G H I J K) (`aonvars')
    gen source = "`sheet'"

    if `i' == 1 {
        save "`all_aon'"
    }
    else {
        append using "`all_aon'"
        save "`all_aon'", replace
    }
}


use "`path'full_dGMS_sGMS_combined.dta", clear
append using "`all_aon'"
save "`path'full_dGMS_sGMS_AON_combined.dta", replace

import excel "`path'dGMS1.xlsx", sheet("dGMS1") cellrange(A4567:R5107) firstrow clear
keep session period costs
tempfile costsdata
save `costsdata'

use "`path'full_dGMS_sGMS_AON_combined.dta", clear
merge m:1 session period using `costsdata'

destring ppnr period decision valuation treatment session StillIn currentprice costs earnings, replace force
generate costid=1 if costs==50
replace costid=2 if costs==70
replace costid=3 if costs==90
replace costid=4 if costs==60
replace costid=5 if costs==80
replace costid=6 if costs==100

drop decisionother time submitted zeros waitpage waitpage2 waitpage3 waitpage4 ready source
drop if session>6 // database specified ordering of costs for more than 6 sessions, which was not needed
drop _merge
sort treatment session period ppnr
replace earnings=valuation-currentprice if treatment==3 & session==5 & period==17 & decision>=currentprice // Earnings were not correctly computed in this instance


preserve
keep treatment session period costs
bysort treatment session period: keep if _n==1
gen producer = 1
gen ppnr = 17
tempfile extra
save `extra'
restore

gen producer = 0
append using `extra'

generate idnr = 1000 * treatment + 100 * session + ppnr

save "`path'all_dGMS_final.dta", replace
sort treatment session period ppnr

//// Adding questionnaire data
local startrow1 1448
local startrow2 1214
local startrow3 1350
local startrow4 1555
local startrow5 1523
local startrow6 1380
local keepvars A B H J K L M
tempfile all_participants

forvalues i = 1/6 {
    local startrow = `startrow`i''
    local endrow = `=`startrow'+15'
    local file = "dGMS`i'.xlsx"
    local sheet = "dGMS`i'"
    local fullpath = "`path'`file'"
    import excel "`fullpath'", sheet("`sheet'") cellrange(A`startrow':M`endrow') clear
    keep `keepvars'
    rename (A B H J K L M) (ppnr producer payoff age female subject experience)
    destring ppnr producer payoff age female subject experience, replace
    gen session = `i'
    gen treatment = 3
    gen idnr = 1000 * treatment + 100 * session + ppnr
    if `i' == 1 {
        save `all_participants'
    }
    else {
        append using `all_participants'
        save `all_participants', replace
    }
}
local sstartrow1 1801
local sstartrow2 1215
local sstartrow3 1274
local sstartrow4 1300
local sstartrow5 1359
local sstartrow6 1876
local skeepvars A B H J K L M

forvalues i = 1/6 {
    local startrow = `sstartrow`i''
    local endrow = `=`startrow'+15'
    local file = "sGMS`i'.xlsx"
    local sheet = "sGMS`i'"
    local fullpath = "`path'`file'"
    import excel "`fullpath'", sheet("`sheet'") cellrange(A`startrow':M`endrow') clear
    keep `skeepvars'
    rename (A B H J K L M) (ppnr producer payoff age female subject experience)
    destring ppnr producer payoff age female subject experience, replace
    gen session = `i'
    gen treatment = 2
    gen idnr = 1000 * treatment + 100 * session + ppnr
    append using `all_participants'
    save `all_participants', replace
}
local astartrow1 1338
local astartrow2 1481
local astartrow3 1327
local astartrow4 1330
local astartrow5 1575
local astartrow6 1318
local akeepvars A B H J K L M

forvalues i = 1/6 {
    local startrow = `astartrow`i''
    local endrow = `=`startrow'+15'
    local file = "AON`i'.xlsx"
    local sheet = "AON`i'"
    local fullpath = "`path'`file'"
    import excel "`fullpath'", sheet("`sheet'") cellrange(A`startrow':M`endrow') clear
    keep `akeepvars'
    rename (A B H J K L M) (ppnr producer payoff age female subject experience)
    destring ppnr producer payoff age female subject experience, replace
    gen session = `i'
    gen treatment = 1
    gen idnr = 1000 * treatment + 100 * session + ppnr
    append using `all_participants'
    save `all_participants', replace
}
replace female = female - 1
replace idnr = 1000 * treatment + 100 * session + 17 if producer==1
save "`path'participant_info.dta", replace


use "`path'all_dGMS_final.dta", clear
merge m:1 idnr using "`path'participant_info.dta"
drop _merge
sort treatment session period ppnr
save "`path'all_dGMS_final_with_participants.dta", replace


generate threshold=56 if (treatment==2 | treatment==3) & costs==50
replace threshold=78 if (treatment==2 | treatment==3) & costs==70
replace threshold=97 if (treatment==2 | treatment==3) & costs==90
replace threshold=60 if (treatment==2 | treatment==3) & costs==60
replace threshold=80 if (treatment==2 | treatment==3) & costs==80
replace threshold=100 if (treatment==2 | treatment==3) & costs==100
replace threshold=50 if treatment==1 & costs==50
replace threshold=78 if treatment==1 & costs==70
replace threshold=97 if treatment==1 & costs==90
replace threshold=60 if treatment==1 & costs==60
replace threshold=80 if treatment==1 & costs==80
replace threshold=100 if treatment==1 & costs==100
generate reservationprice=11 if (treatment==2 | treatment==3) & costs==50
replace reservationprice=11 if (treatment==2 | treatment==3) & costs==70
replace reservationprice=11 if (treatment==2 | treatment==3) & costs==90
replace reservationprice=0 if (treatment==2 | treatment==3) & costs==60
replace reservationprice=0 if (treatment==2 | treatment==3) & costs==80
replace reservationprice=0 if (treatment==2 | treatment==3) & costs==100
replace reservationprice=11 if treatment==1 & costs==50
replace reservationprice=11 if treatment==1 & costs==70
replace reservationprice=12 if treatment==1 & costs==90
replace reservationprice=10 if treatment==1 & costs==60
replace reservationprice=10 if treatment==1 & costs==80
replace reservationprice=11 if treatment==1 & costs==100

generate nowprice=. if treatment==2
forvalues i=1/15 {
generate price_`i'=threshold/`i' if treatment==2
replace price_`i'=reservationprice if reservationprice>price_`i' & treatment==2
generate higher_`i'=0 if treatment==2 & producer==0
replace higher_`i'=1 if decision>=price_`i' & treatment==2 & producer==0
sort treatment session period ppnr
bysort treatment session period : egen mbackers_`i' = total(higher_`i') if treatment==2 & producer==0
replace nowprice=ceil(price_`i') if mbackers_`i'>=`i' & treatment==2 & producer==0
replace nowprice=reservationprice if reservationprice>price_`i' & mbackers_`i'>=`i' & treatment==2 & producer==0
drop price_`i' higher_`i' mbackers_`i'
}
generate obtained=0 if treatment==2
replace obtained=1 if treatment==2 & decision>=nowprice & nowprice!=.
gen payment=.
replace payment=obtained*nowprice if treatment==2 & nowprice!=.

gen potentialpayment = currentprice * StillIn if treatment==3
replace potentialpayment = decision if treatment==1
replace potentialpayment = payment if treatment==2
bysort treatment session period: egen potentialtotalpayment = total(potentialpayment)
gen success=potentialtotalpayment>=threshold
replace payment=potentialpayment if success==1 & (treatment==1 | treatment==3)
bysort treatment session period: egen totalpayment = total(payment)
generate profit=max(totalpayment-costs,0) if producer==1
generate profitobjective=0
replace profitobjective=1 if costs==50 | costs==70 | costs==90

replace obtained=1 if success==1 & treatment==1 & decision>=reservationprice & producer==0
replace obtained=1 if success==1 & (treatment==2 | treatment==3) & payment>0 & producer==0
generate indivsur=obtained*valuation

bysort treatment session period: egen totalindivsur=total(indivsur)
generate surplus=(totalindivsur-costs)*success


//// Consumer Behavior to analyze deviations from theoretical predictions
generate bnequal=0 if producer==0 & treatment==1
replace bnequal=1 if producer==0 & treatment==1 & valuation<reservationprice & decision==0
replace bnequal=1 if producer==0 & treatment==1 & valuation>=reservationprice & decision==reservationprice & costs==50
replace bnequal=1 if producer==0 & treatment==1 & valuation>=reservationprice & decision==reservationprice & costs==60
replace bnequal=1 if producer==0 & treatment==1 & valuation>=reservationprice & decision==reservationprice & costs==80
replace bnequal=1 if producer==0 & treatment==1 & valuation>=11 & valuation<=19 & decision==11 & costs==70
replace bnequal=1 if producer==0 & treatment==1 & valuation>=19 & decision==12 & costs==70
replace bnequal=1 if producer==0 & treatment==1 & valuation>=12 & valuation<=19 & decision==12 & costs==90
replace bnequal=1 if producer==0 & treatment==1 & valuation>=19 & decision==13 & costs==90
replace bnequal=1 if producer==0 & treatment==1 & valuation>=11 & valuation<=19 & decision==11 & costs==100
replace bnequal=1 if producer==0 & treatment==1 & valuation>=19 & decision==12 & costs==100

generate bninline=bnequal
replace bninline=1 if producer==0 & treatment==1 & valuation<=reservationprice & decision==0

generate bnhigher=0 if producer==0 & treatment==1
replace bnhigher=1 if producer==0 & treatment==1 & valuation<reservationprice & decision>0
replace bnhigher=1 if producer==0 & treatment==1 & valuation>=reservationprice & decision>reservationprice & costs==50
replace bnhigher=1 if producer==0 & treatment==1 & valuation>=reservationprice & decision>reservationprice & costs==60
replace bnhigher=1 if producer==0 & treatment==1 & valuation>=reservationprice & decision>reservationprice & costs==80
replace bnhigher=1 if producer==0 & treatment==1 & valuation>=11 & valuation<19 & decision>11 & costs==70
replace bnhigher=1 if producer==0 & treatment==1 & valuation>=19 & decision>12 & costs==70
replace bnhigher=1 if producer==0 & treatment==1 & valuation>=12 & valuation<19 & decision>12 & costs==90
replace bnhigher=1 if producer==0 & treatment==1 & valuation>=19 & decision>13 & costs==90
replace bnhigher=1 if producer==0 & treatment==1 & valuation>=11 & valuation<19 & decision>11 & costs==100
replace bnhigher=1 if producer==0 & treatment==1 & valuation>=19 & decision>12 & costs==100

generate bnlower=0 if producer==0 & treatment==1
replace bnlower=1 if producer==0 & treatment==1 & valuation>=reservationprice & decision<reservationprice & costs==50
replace bnlower=1 if producer==0 & treatment==1 & valuation>=reservationprice & decision<reservationprice & costs==60
replace bnlower=1 if producer==0 & treatment==1 & valuation>=reservationprice & decision<reservationprice & costs==80
replace bnlower=1 if producer==0 & treatment==1 & valuation>=11 & valuation<=19 & decision<11 & costs==70
replace bnlower=1 if producer==0 & treatment==1 & valuation>19 & decision<12 & costs==70
replace bnlower=1 if producer==0 & treatment==1 & valuation>=12 & valuation<=19 & decision<12 & costs==90
replace bnlower=1 if producer==0 & treatment==1 & valuation>19 & decision<13 & costs==90
replace bnlower=1 if producer==0 & treatment==1 & valuation>=11 & valuation<=19 & decision<11 & costs==100
replace bnlower=1 if producer==0 & treatment==1 & valuation>19 & decision<12 & costs==100
replace bnlower=1 if producer==0 & treatment==1 & valuation==reservationprice & decision==0
generate bntoolow=bnlower if treatment==1
replace bntoolow=0 if treatment==1 & valuation==reservationprice & decision==0

generate bn=0 if producer==0 & treatment==1
replace bn=0 if producer==0 & treatment==1 & valuation<reservationprice
replace bn=11 if producer==0 & treatment==1 & valuation>=reservationprice & costs==50
replace bn=10 if producer==0 & treatment==1 & valuation>=reservationprice & costs==60
replace bn=10 if producer==0 & treatment==1 & valuation>=reservationprice & costs==80
replace bn=11 if producer==0 & treatment==1 & valuation>=11 & valuation<19 & costs==70
replace bn=11.5 if producer==0 & treatment==1 & valuation==19 & costs==70
replace bn=12 if producer==0 & treatment==1 & valuation==20 & costs==70
replace bn=12 if producer==0 & treatment==1 & valuation>=12 & valuation<19 & costs==90
replace bn=12.5 if producer==0 & treatment==1 & valuation==19 & costs==90
replace bn=13 if producer==0 & treatment==1 & valuation==20 & costs==90
replace bn=11 if producer==0 & treatment==1 & valuation>=11 & valuation<19 & costs==100
replace bn=11.5 if producer==0 & treatment==1 & valuation==19 & costs==100
replace bn=12 if producer==0 & treatment==1 & valuation==20 & costs==100

generate bidisvalue=0 if producer==0
replace bidisvalue=1 if producer==0 & decision==valuation
generate bidgrvalue=0 if producer==0
replace bidgrvalue=1 if producer==0 & valuation<decision
generate bidsmvalue=0 if producer==0
replace bidsmvalue=1 if producer==0 & decision<valuation
generate bid1smvalue=0 if producer==0
replace bid1smvalue=1 if producer==0 & decision==valuation-1
generate bidisr=0 if producer==0
replace bidisr=1 if producer==0 & decision==reservationprice
generate lowvb=0 if producer==0
replace lowvb=1 if producer==0 & valuation<ceil(threshold/15) & decision<ceil(threshold/15)

generate abcd=0 if treatment==2 & producer==0
generate toolowq=0 if treatment==2 & producer==0
generate toohighq=0 if treatment==2 & producer==0
generate pricenow=0 if treatment==2 & producer==0

forvalues i=1/15 {
replace pricenow=ceil(threshold/`i') if treatment==2 & producer==0
replace pricenow=reservationprice if reservationprice>pricenow & treatment==2 & producer==0
replace abcd=abcd+1 if treatment==2 & producer==0 & ((valuation>pricenow & pricenow>decision)|(valuation<pricenow & pricenow<=decision))
replace toolowq=toolowq+1 if treatment==2 & producer==0 & valuation>pricenow & pricenow>decision
replace toohighq=toohighq+1 if treatment==2 & producer==0 & valuation<pricenow & pricenow<=decision

}
generate nodev=0 if producer==0 & treatment==2
replace nodev=1 if producer==0 & treatment==2 & abcd==0
generate bestresponse=0 if producer==0 & treatment==2
replace bestresponse=lowvb+bidisvalue+bid1smvalue+nodev if producer==0 & treatment==2
replace bestresponse=1 if producer==0 & treatment==2 & bestresponse>1

generate weaklydominated=1-bestresponse if producer==0 & treatment==2

sort treatment session period earnings
generate pricegms=valuation-earnings if (treatment==2 | treatment==3) & success==1 & decision>=valuation-earnings & earnings>0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0
replace pricegms = pricegms[_n+1] if missing(pricegms) & producer==0


generate decisionb=decision
replace decisionb=pricegms if decision>pricegms & pricegms!=.

sort treatment session period decision
generate sumatmoment=16-mod(_n,16) if ppnr!=17
replace sumatmoment=30*sumatmoment if ppnr!=17
replace sumatmoment=sumatmoment[_n-1] if decision==decision[_n-1]
replace sumatmoment=. if treatment==1
generate autostop=0 if treatment==2 & producer==0
replace autostop=1 if sumatmoment<threshold & treatment==2 & producer==0

generate abcdj=0 if (treatment==2 | treatment==3) & producer==0
generate toolowqj=0 if (treatment==2 | treatment==3) & producer==0
generate toohighqj=0 if (treatment==2 | treatment==3) & producer==0
generate pricenowj=0 if (treatment==2 | treatment==3) & producer==0
replace StillIn=0 if treatment==2 & producer==0
replace StillIn=1 if treatment==2 & producer==0 & ((decision>=pricegms & pricegms!=.) | autostop==1)


forvalues i=1/15 {
replace pricenowj=ceil(threshold/`i') if (treatment==2 | treatment==3) & producer==0
replace pricenowj=reservationprice if reservationprice>pricenowj & (treatment==2 | treatment==3) & producer==0
replace abcdj=abcdj+1 if treatment==3 & producer==0 & ((valuation>pricenowj & pricenowj>decision & StillIn==0)|(valuation<pricenowj & pricenowj<=decision)) & treatment==3
replace toolowqj=toolowqj+1 if treatment==3 & producer==0 & valuation>pricenowj & pricenowj>decision & treatment==3
replace toohighqj=toohighqj+1 if treatment==3 & producer==0 & valuation<pricenowj & pricenowj<=decision & treatment==3
replace abcdj=abcdj+1 if treatment==2 & producer==0 & ((valuation>pricenowj & pricenowj>decision & StillIn==0)|(valuation<pricenowj & pricenowj<=decisionb)) & treatment==2
replace toolowqj=toolowqj+1 if treatment==2 & producer==0 & valuation>pricenowj & pricenowj>decisionb & treatment==2
replace toohighqj=toohighqj+1 if treatment==2 & producer==0 & valuation<pricenowj & pricenowj<=decisionb & treatment==2
}
generate nodevj=0 if producer==0 & (treatment==2 | treatment==3)
replace nodevj=1 if producer==0 & (treatment==2 | treatment==3) & abcdj==0

generate bestresponsej=0 if producer==0 & (treatment==2 | treatment==3)
replace bestresponsej=lowvb+bidisvalue+bid1smvalue+nodevj if producer==0 & (treatment==2 | treatment==3)
replace bestresponsej=1 if producer==0 & (treatment==2 | treatment==3) & bestresponsej>1

generate toolowj=0 if producer==0 & (treatment==2 | treatment==3)
replace toolowj=1 if producer==0 & (treatment==2 | treatment==3) & toolowqj>0
replace toolowj=0 if bestresponsej==1
generate toohighj=0 if producer==0 & (treatment==2 | treatment==3)
replace toohighj=1 if producer==0 & (treatment==2 | treatment==3) & toohighqj>0
generate toolow=0 if producer==0 & (treatment==2 | treatment==3)
replace toolow=1 if producer==0 & (treatment==2 | treatment==3) & toolowq>0
generate toohigh=0 if producer==0 & (treatment==2 | treatment==3)
replace toohigh=1 if producer==0 & (treatment==2 | treatment==3) & toohighq>0
}

//// Results
//// Section 5.1 - Profit Objective
mean profit if period>15 & profitobjective==1, over(treatment)
preserve
collapse (mean) realprofitbysession= profit if period>15 & profitobjective==1, by(treatment session)
reshape wide realprofitbysession, i(session) j(treatment) 
summarize realprofitbysession1 realprofitbysession2 realprofitbysession3
gen Diff12=realprofitbysession1-realprofitbysession2
summarize Diff12
permtest1 Diff12=0
gen Diff13=realprofitbysession1-realprofitbysession3
summarize Diff13
permtest1 Diff13=0
gen Diff23=realprofitbysession2-realprofitbysession3
summarize Diff23
permtest1 Diff23=0
restore

mean surplus if period>15 & profitobjective==1, over(treatment)
preserve
collapse (mean) realsurplusbysession= surplus if period>15 & profitobjective==1, by(treatment session)
reshape wide realsurplusbysession, i(session) j(treatment) 
summarize realsurplusbysession1 realsurplusbysession2 realsurplusbysession3
gen Diff12=realsurplusbysession1-realsurplusbysession2
permtest1 Diff12=0
gen Diff13=realsurplusbysession1-realsurplusbysession3
permtest1 Diff13=0
gen Diff23=realsurplusbysession2-realsurplusbysession3
permtest1 Diff23=0
restore


bysort treatment session: egen realprofit2=mean(profit) if producer==1 & profitobjective==1 & period>15
order realprofit2
gsort treatment session  - period
sort treatment session realprofit2
replace realprofit2=. if realprofit2[_n+1]==realprofit2 & treatment[_n+1]==treatment & session[_n+1]==session

preserve
collapse (mean) meanrealprofit= realprofit2 (sd) sdrealprofit=realprofit2 (count) n=realprofit2 if profitobjective==1 & period>15, by(treatment)
generate hirealprofit = meanrealprofit + invttail(n-1,0.025)*(sdrealprofit / sqrt(n))
generate lorealprofit = meanrealprofit - invttail(n-1,0.025)*(sdrealprofit / sqrt(n))
sort treatment

twoway ///
    (bar meanrealprofit treatment if treatment==1, bcolor(navy) fcolor(navy) lcolor(white)) ///
    (bar meanrealprofit treatment if treatment==2, bcolor("orange_red") fcolor("orange_red") lcolor(white)) ///
    (bar meanrealprofit treatment if treatment==3, bcolor("dkorange") fcolor("dkorange") lcolor(white)) ///
    (rcap hirealprofit lorealprofit treatment, lcolor(black) lwidth(medium)) ///
    (scatteri 14.44 2.1 14.64 2.1, recast(line) lcolor(black)) ///
    (scatteri 14.44 2.9 14.64 2.9, recast(line) lcolor(black)) ///
    (scatteri 14.64 2.1 14.64 2.9, recast(line) lcolor(black)) ///
    (scatteri 14.84 2.5 (0)"**", msymbol(none) lcolor(black)) ///
    (scatteri 15.8477 0.5 15.8477 1.5, recast(line) lpattern(dash) lcolor(black)) ///
    (scatteri 16.2740 1.5 16.2740 3.5, recast(line) lpattern(dash) lcolor(black)), ///
    ylabel(0(5)20, labsize(large) angle(0) nogrid) ///
    xlabel(1 "AON" 2 "sGMS" 3 "dGMS", labsize(large)) ///
    xtitle("Profit", size(huge) margin(small) color(black)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    saving(b1, replace)
restore

bysort treatment session: egen realsurplus2=mean(surplus) if producer==1 & profitobjective==1 & period>15
order realsurplus2
gsort treatment session  - period
sort treatment session realsurplus2
replace realsurplus2=. if realsurplus2[_n+1]==realsurplus2 & treatment[_n+1]==treatment & session[_n+1]==session

preserve
collapse (mean) meanrealsurplus= realsurplus2 (sd) sdrealsurplus=realsurplus2 (count) n=realsurplus2 if profitobjective==1 & period>15, by(treatment)
generate hirealsurplus = meanrealsurplus + invttail(n-1,0.025)*(sdrealsurplus / sqrt(n))
generate lorealsurplus = meanrealsurplus - invttail(n-1,0.025)*(sdrealsurplus / sqrt(n))
sort treatment

twoway ///
    (bar meanrealsurplus treatment if treatment==1, bcolor(navy) fcolor(navy) lcolor(white)) ///
    (bar meanrealsurplus treatment if treatment==2, bcolor("orange_red") fcolor("orange_red") lcolor(white)) ///
    (bar meanrealsurplus treatment if treatment==3, bcolor("dkorange") fcolor("dkorange") lcolor(white)) ///
    (rcap hirealsurplus lorealsurplus treatment, lcolor(black) lwidth(medium)) ///
    (scatteri 31.64 2.1 32.14 2.1, recast(line) lcolor(black)) ///
    (scatteri 31.64 2.9 32.14 2.9, recast(line) lcolor(black)) ///
    (scatteri 32.14 2.1 32.14 2.9, recast(line) lcolor(black)) ///
    (scatteri 32.64 2.5 (0)"**", msymbol(none) lcolor(black)) ///
    (scatteri 35.8666 0.5 35.8666 1.5, recast(line) lpattern(dash) lcolor(black)) ///
    (scatteri 37.5774 1.5 37.5774 3.5, recast(line) lpattern(dash) lcolor(black)), ///
    ylabel(0(10)50, labsize(large) angle(0) nogrid) ///
    xlabel(1 "AON" 2 "sGMS" 3 "dGMS", labsize(large)) ///
    xtitle("Surplus", size(huge) margin(small) color(black)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    saving(b2, replace)
restore

graph combine "b1" "b2", xsize(5.5) ysize(3) graphregion(color(white))
drop realprofit2 realsurplus2


//// Section 5.2 - Success Objective
mean success if period>15 & profitobjective==0, over(treatment)
preserve
collapse (mean) realsuccessbysession= success if period>15 & profitobjective==0, by(treatment session)
reshape wide realsuccessbysession, i(session) j(treatment) 
gen Diff12=realsuccessbysession1-realsuccessbysession2
summarize Diff12
permtest1 Diff12=0
gen Diff13=realsuccessbysession1-realsuccessbysession3
permtest1 Diff13=0
gen Diff23=realsuccessbysession2-realsuccessbysession3
permtest1 Diff23=0
restore

mean surplus if period>15 & profitobjective==0, over(treatment)
preserve
collapse (mean) realsurplusbysession= surplus if period>15 & profitobjective==0, by(treatment session)
reshape wide realsurplusbysession, i(session) j(treatment) 
gen Diff12=realsurplusbysession1-realsurplusbysession2
permtest1 Diff12=0
gen Diff13=realsurplusbysession1-realsurplusbysession3
permtest1 Diff13=0
gen Diff23=realsurplusbysession2-realsurplusbysession3
permtest1 Diff23=0
restore


bysort treatment session: egen realsuccess2=mean(success) if producer==1 & profitobjective==0 & period>15
order realsuccess2
gsort treatment session  - period
sort treatment session realsuccess2
replace realsuccess2=. if realsuccess2[_n+1]==realsuccess2 & treatment[_n+1]==treatment & session[_n+1]==session

preserve
collapse (mean) meanrealsuccess= realsuccess2 (sd) sdrealsuccess=realsuccess2 (count) n=realsuccess2 if profitobjective==0 & period>15, by(treatment)
generate hirealsuccess = meanrealsuccess + invttail(n-1,0.025)*(sdrealsuccess / sqrt(n))
generate lorealsuccess = meanrealsuccess - invttail(n-1,0.025)*(sdrealsuccess / sqrt(n))
sort treatment	 

twoway ///
    (bar meanrealsuccess treatment if treatment==1, bcolor(navy) fcolor(navy) lcolor(white)) ///
    (bar meanrealsuccess treatment if treatment==2, bcolor("orange_red") fcolor("orange_red") lcolor(white)) ///
    (bar meanrealsuccess treatment if treatment==3, bcolor("dkorange") fcolor("dkorange") lcolor(white)) ///
    (rcap hirealsuccess lorealsuccess treatment, lcolor(black) lwidth(medium)) ///
    (scatteri .5591 0.5 .5591 1.5, recast(line) lpattern(dash) lcolor(black)) ///
    (scatteri .6519 1.5 .6519 3.5, recast(line) lpattern(dash) lcolor(black)), ///
    ylabel(0(0.1)0.7, labsize(large) angle(0) nogrid) ///
    xlabel(1 "AON" 2 "sGMS" 3 "dGMS", labsize(large)) ///
    xtitle("Success", size(huge) margin(small) color(black)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(lcolor(white)) ///
    saving(b3, replace)
restore


bysort treatment session: egen realsurplus4=mean(surplus) if producer==1 & profitobjective==0 & period>15
order realsurplus4
gsort treatment session  - period
sort treatment session realsurplus4
replace realsurplus4=. if realsurplus4[_n+1]==realsurplus4 & treatment[_n+1]==treatment & session[_n+1]==session

preserve
collapse (mean) meanrealsurplus= realsurplus4 (sd) sdrealsurplus=realsurplus4 (count) n=realsurplus4 if profitobjective==0 & period>15, by(treatment)
generate hirealsurplus = meanrealsurplus + invttail(n-1,0.025)*(sdrealsurplus / sqrt(n))
generate lorealsurplus = meanrealsurplus - invttail(n-1,0.025)*(sdrealsurplus / sqrt(n))
sort treatment	 

twoway ///
    (bar meanrealsurplus treatment if treatment==1, bcolor(navy) fcolor(navy) lcolor(white)) ///
    (bar meanrealsurplus treatment if treatment==2, bcolor("orange_red") fcolor("orange_red") lcolor(white)) ///
    (bar meanrealsurplus treatment if treatment==3, bcolor("dkorange") fcolor("dkorange") lcolor(white)) ///
    (rcap hirealsurplus lorealsurplus treatment, lcolor(black) lwidth(medium)) ///
    (scatteri 48.5 1.1 49.0 1.1, recast(line) lcolor(black)) ///
    (scatteri 48.5 2.9 49.0 2.9, recast(line) lcolor(black)) ///
    (scatteri 49.0 1.1 49.0 2.9, recast(line) lcolor(black)) ///
    (scatteri 49.5 2 (0)"**", msymbol(none) lcolor(black)) ///
    (scatteri 38.39 2.1 38.89 2.1, recast(line) lcolor(black)) ///
    (scatteri 38.39 2.9 38.89 2.9, recast(line) lcolor(black)) ///
    (scatteri 38.89 2.1 38.89 2.9, recast(line) lcolor(black)) ///
    (scatteri 39.39 2.5 (0)"**", msymbol(none) lcolor(black)) ///
    (scatteri 33.8395 0.5 33.8395 1.5, recast(line) lpattern(dash) lcolor(black)) ///
    (scatteri 46.7272 1.5 46.7272 3.5, recast(line) lpattern(dash) lcolor(black)), ///
    ylabel(0(10)50, labsize(large) angle(0) nogrid) ///
    xlabel(1 "AON" 2 "sGMS" 3 "dGMS", labsize(large)) ///
    xtitle("Surplus", size(huge) margin(small) color(black)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(lcolor(white)) ///
    saving(b4, replace)
restore

graph combine "b3" "b4", xsize(5.5) ysize(3) graphregion(color(white))
drop realsuccess2 realsurplus4

//// Section 5.3 - Pooled Objecitves
bysort treatment session period: egen sumvaluation=total(valuation)
gen meanvaluation = sumvaluation / 15

/// Table
mean profit success surplus if treatment==1 & period>15
tobit profit ib1.treatment c.threshold c.reservationprice meanvaluation if period>15 & producer==1, ll(0) vce(cluster idnr)
probit success ib1.treatment c.threshold c.reservationprice meanvaluation if period>15 & producer==1, vce(cluster idnr)
margins, dydx(*)
tobit surplus ib1.treatment c.threshold c.reservationprice meanvaluation if period>15 & producer==1, ll(0) vce(cluster idnr)

/// p-values
tobit profit ib3.treatment c.threshold c.reservationprice meanvaluation if period>15 & producer==1, ll(0) vce(cluster idnr)
* For 1.treatment:
scalar b1 = _b[1.treatment]
scalar se1 = _se[1.treatment]
scalar t1 = b1/se1
scalar p_twosided1 = 2 * ttail(e(df_r), abs(t1))
* For H₀: beta >= 0 vs H₁: beta < 0:
scalar p_one1 = cond(b1 < 0, p_twosided1/2, 1 - p_twosided1/2)
display "One-sided p-value for 1.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one1
* For 2.treatment:
scalar b2 = _b[2.treatment]
scalar se2 = _se[2.treatment]
scalar t2 = b2/se2
scalar p_twosided2 = 2 * ttail(e(df_r), abs(t2))
scalar p_one2 = cond(b2 < 0, p_twosided2/2, 1 - p_twosided2/2)
display "One-sided p-value for 2.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one2


probit success ib3.treatment c.threshold c.reservationprice meanvaluation if period>15 & producer==1, vce(cluster idnr)
margins, dydx(*)
matrix list r(table)
scalar m1 = r(table)[1, colnumb(r(table), "1.treatment")]
scalar se1 = r(table)[2, colnumb(r(table), "1.treatment")]
scalar t1 = m1/se1
scalar p_one = cond(m1 < 0, normal(t1), 1 - normal(t1))
display "One-sided p-value for marginal effect of 1.treatment: " p_one
matrix list r(table)
scalar m2 = r(table)[1, colnumb(r(table), "2.treatment")]
scalar se2 = r(table)[2, colnumb(r(table), "2.treatment")]
scalar t2 = m2 / se2
scalar p_one2 = cond(m2 < 0, normal(t2), 1 - normal(t2))
display "One-sided p-value for marginal effect of 2.treatment: " p_one2


tobit surplus ib3.treatment c.threshold c.reservationprice meanvaluation if period>15 & producer==1, ll(0) vce(cluster idnr)
* For 1.treatment:
scalar b1 = _b[1.treatment]
scalar se1 = _se[1.treatment]
scalar t1 = b1/se1
scalar p_twosided1 = 2 * ttail(e(df_r), abs(t1))
* For H₀: beta >= 0 vs H₁: beta < 0:
scalar p_one1 = cond(b1 < 0, p_twosided1/2, 1 - p_twosided1/2)
display "One-sided p-value for 1.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one1
* For 2.treatment:
scalar b2 = _b[2.treatment]
scalar se2 = _se[2.treatment]
scalar t2 = b2/se2
scalar p_twosided2 = 2 * ttail(e(df_r), abs(t2))
scalar p_one2 = cond(b2 < 0, p_twosided2/2, 1 - p_twosided2/2)
display "One-sided p-value for 2.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one2

tobit profit ib2.treatment c.threshold c.reservationprice meanvaluation if period>15 & producer==1, ll(0) vce(cluster idnr)
* For 1.treatment:
scalar b1 = _b[1.treatment]
scalar se1 = _se[1.treatment]
scalar t1 = b1/se1
scalar p_twosided1 = 2 * ttail(e(df_r), abs(t1))
* For H₀: beta >= 0 vs H₁: beta < 0:
scalar p_one1 = cond(b1 < 0, p_twosided1/2, 1 - p_twosided1/2)
display "One-sided p-value for 1.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one1

probit success ib2.treatment c.threshold c.reservationprice meanvaluation if period>15 & producer==1, vce(cluster idnr)
margins, dydx(*)
matrix list r(table)
scalar m1 = r(table)[1, colnumb(r(table), "1.treatment")]
scalar se1 = r(table)[2, colnumb(r(table), "1.treatment")]
scalar t1 = m1/se1
scalar p_one = cond(m1 > 0, normal(t1), 1 - normal(t1))
display "One-sided p-value for marginal effect of 1.treatment: " p_one

tobit surplus ib2.treatment c.threshold c.reservationprice meanvaluation if period>15 & producer==1, ll(0) vce(cluster idnr)
* For 1.treatment:
scalar b1 = _b[1.treatment]
scalar se1 = _se[1.treatment]
scalar t1 = b1/se1
scalar p_twosided1 = 2 * ttail(e(df_r), abs(t1))
* For H₀: beta >= 0 vs H₁: beta < 0:
scalar p_one1 = cond(b1 < 0, p_twosided1/2, 1 - p_twosided1/2)
display "One-sided p-value for 1.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one1



//// Section 5.4 - Consumer Behavior
regress decision valuation c.threshold c.reservationprice if treatment==1 & valuation>reservationprice & period>15, vce (cluster idnr)

generate valdecision=valuation*1000+decision
egen freq = sum(1) if !missing(valdecision) & period>15, by(treatment costid valdecision)
egen freqdyn = sum(1) if !missing(valdecision) & period>15, by(treatment costid valdecision bestresponsej)


label define costidlbl 1 "T = 56,  r = 11" 2 "T = 78,  r = 11" 3 "T = 97,  r = 11" ///
   4 "T = 60,  r = 0" 5 "T = 80,  r = 0" 6 "T = 100, r = 0"
label values costid costidlbl


twoway ///
    (scatter decision valuation [w=freq] if treatment==1 & period>15, ///
        msymbol(O) mcolor(gs10) msize(tiny)) ///
    (scatter decision valuation [w=freq] if treatment==1 & bninline==0 & period>15, ///
        msymbol(O) mcolor(black) msize(tiny)), ///
    by(costid, note("") legend(off) title("") cols(3) yrescale xrescale ///
        graphregion(color(white)) ///
        plotregion(color(white))) ///
    xlabel(, labsize(medium) nogrid) ///
    ylabel(0(5)20, angle(horizontal) labsize(medium) nogrid) ///
    yscale(range(0 20)) ///
    xtitle("Value", size(large)) ///
    ytitle("Bid", size(large)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white))


twoway ///
    (scatter decision valuation [w=freq] if treatment==2 & period>15, ///
        msymbol(O) mcolor(gs10) msize(tiny)) ///
    (scatter decision valuation [w=freq] if treatment==2 & weaklydominated==1 & period>15, ///
        msymbol(O) mcolor(black) msize(tiny)), ///
    by(costid, note("") legend(off) title("") cols(3) yrescale xrescale ///
        graphregion(color(white)) ///
        plotregion(color(white))) ///
    xlabel(, labsize(medium) nogrid) ///
    ylabel(, angle(horizontal) labsize(medium) nogrid) ///
    xtitle("Value", size(large)) ///
    ytitle("Bid", size(large)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white))


twoway ///
    (scatter decision valuation [w=freq] if treatment==3 & period>15, ///
        msymbol(O) mcolor(gs10) msize(tiny)) ///
    (scatter decision valuation [w=freqdyn] if treatment==3 & bestresponsej==0 & period>15, ///
        msymbol(O) mcolor(black) msize(tiny)), ///
    by(costid, note("") legend(off) title("") cols(3) yrescale xrescale ///
        graphregion(color(white)) ///
        plotregion(color(white))) ///
    xlabel(, labsize(medium) nogrid) ///
    ylabel(0(5)20, angle(horizontal) labsize(medium) nogrid) ///
    yscale(range(0 20)) ///
    xtitle("Value", size(large)) ///
    ytitle("Bid", size(large)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white))

//// Online Appendix C - Additional Tables and Figures

//// Profit Objective All Rounds
preserve
collapse (mean) realprofitbysession= profit if profitobjective==1, by(treatment session)
reshape wide realprofitbysession, i(session) j(treatment) 
summarize realprofitbysession1 realprofitbysession2 realprofitbysession3
gen Diff12=realprofitbysession1-realprofitbysession2
summarize Diff12
permtest1 Diff12=0
gen Diff13=realprofitbysession1-realprofitbysession3
summarize Diff13
permtest1 Diff13=0
gen Diff23=realprofitbysession2-realprofitbysession3
summarize Diff23
permtest1 Diff23=0
restore

preserve
collapse (mean) realsurplusbysession= surplus if profitobjective==1, by(treatment session)
reshape wide realsurplusbysession, i(session) j(treatment) 
summarize realsurplusbysession1 realsurplusbysession2 realsurplusbysession3
gen Diff12=realsurplusbysession1-realsurplusbysession2
permtest1 Diff12=0
gen Diff13=realsurplusbysession1-realsurplusbysession3
permtest1 Diff13=0
gen Diff23=realsurplusbysession2-realsurplusbysession3
permtest1 Diff23=0
restore


bysort treatment session: egen realprofit2=mean(profit) if producer==1 & profitobjective==1
order realprofit2
gsort treatment session  - period
sort treatment session realprofit2
replace realprofit2=. if realprofit2[_n+1]==realprofit2 & treatment[_n+1]==treatment & session[_n+1]==session

preserve
collapse (mean) meanrealprofit= realprofit2 (sd) sdrealprofit=realprofit2 (count) n=realprofit2 if profitobjective==1, by(treatment)
generate hirealprofit = meanrealprofit + invttail(n-1,0.025)*(sdrealprofit / sqrt(n))
generate lorealprofit = meanrealprofit - invttail(n-1,0.025)*(sdrealprofit / sqrt(n))
sort treatment

twoway ///
    (bar meanrealprofit treatment if treatment==1, bcolor(navy) fcolor(navy) lcolor(white)) ///
    (bar meanrealprofit treatment if treatment==2, bcolor("orange_red") fcolor("orange_red") lcolor(white)) ///
    (bar meanrealprofit treatment if treatment==3, bcolor("dkorange") fcolor("dkorange") lcolor(white)) ///
    (rcap hirealprofit lorealprofit treatment, lcolor(black) lwidth(medium)) ///
    (scatteri 15.8477 0.5 15.8477 1.5, recast(line) lpattern(dash) lcolor(black)) ///
    (scatteri 16.2740 1.5 16.2740 3.5, recast(line) lpattern(dash) lcolor(black)), ///
    ylabel(0(5)20, labsize(large) angle(0) nogrid) ///
    xlabel(1 "AON" 2 "sGMS" 3 "dGMS", labsize(large)) ///
    xtitle("Profit", size(huge) margin(small) color(black)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    saving(c1, replace)
restore

bysort treatment session: egen realsurplus2=mean(surplus) if producer==1 & profitobjective==1
order realsurplus2
gsort treatment session  - period
sort treatment session realsurplus2
replace realsurplus2=. if realsurplus2[_n+1]==realsurplus2 & treatment[_n+1]==treatment & session[_n+1]==session

preserve
collapse (mean) meanrealsurplus= realsurplus2 (sd) sdrealsurplus=realsurplus2 (count) n=realsurplus2 if profitobjective==1, by(treatment)
generate hirealsurplus = meanrealsurplus + invttail(n-1,0.025)*(sdrealsurplus / sqrt(n))
generate lorealsurplus = meanrealsurplus - invttail(n-1,0.025)*(sdrealsurplus / sqrt(n))
sort treatment

twoway ///
    (bar meanrealsurplus treatment if treatment==1, bcolor(navy) fcolor(navy) lcolor(white)) ///
    (bar meanrealsurplus treatment if treatment==2, bcolor("orange_red") fcolor("orange_red") lcolor(white)) ///
    (bar meanrealsurplus treatment if treatment==3, bcolor("dkorange") fcolor("dkorange") lcolor(white)) ///
    (rcap hirealsurplus lorealsurplus treatment, lcolor(black) lwidth(medium)) ///
    (scatteri 35.8666 0.5 35.8666 1.5, recast(line) lpattern(dash) lcolor(black)) ///
    (scatteri 37.5774 1.5 37.5774 3.5, recast(line) lpattern(dash) lcolor(black)), ///
    ylabel(0(10)50, labsize(large) angle(0) nogrid) ///
    xlabel(1 "AON" 2 "sGMS" 3 "dGMS", labsize(large)) ///
    xtitle("Surplus", size(huge) margin(small) color(black)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    saving(c2, replace)
restore

graph combine "c1" "c2", xsize(5.5) ysize(3) graphregion(color(white))
drop realprofit2 realsurplus2

//// Success Objective All Rounds
preserve
collapse (mean) realsuccessbysession= success if profitobjective==0, by(treatment session)
reshape wide realsuccessbysession, i(session) j(treatment) 
gen Diff12=realsuccessbysession1-realsuccessbysession2
summarize Diff12
permtest1 Diff12=0
gen Diff13=realsuccessbysession1-realsuccessbysession3
permtest1 Diff13=0
gen Diff23=realsuccessbysession2-realsuccessbysession3
permtest1 Diff23=0
restore

preserve
collapse (mean) realsurplusbysession= surplus if profitobjective==0, by(treatment session)
reshape wide realsurplusbysession, i(session) j(treatment) 
gen Diff12=realsurplusbysession1-realsurplusbysession2
permtest1 Diff12=0
gen Diff13=realsurplusbysession1-realsurplusbysession3
permtest1 Diff13=0
gen Diff23=realsurplusbysession2-realsurplusbysession3
permtest1 Diff23=0
restore


bysort treatment session: egen realsuccess2=mean(success) if producer==1 & profitobjective==0
order realsuccess2
gsort treatment session  - period
sort treatment session realsuccess2
replace realsuccess2=. if realsuccess2[_n+1]==realsuccess2 & treatment[_n+1]==treatment & session[_n+1]==session

preserve
collapse (mean) meanrealsuccess= realsuccess2 (sd) sdrealsuccess=realsuccess2 (count) n=realsuccess2 if profitobjective==0, by(treatment)
generate hirealsuccess = meanrealsuccess + invttail(n-1,0.025)*(sdrealsuccess / sqrt(n))
generate lorealsuccess = meanrealsuccess - invttail(n-1,0.025)*(sdrealsuccess / sqrt(n))
sort treatment	 

twoway ///
    (bar meanrealsuccess treatment if treatment==1, bcolor(navy) fcolor(navy) lcolor(white)) ///
    (bar meanrealsuccess treatment if treatment==2, bcolor("orange_red") fcolor("orange_red") lcolor(white)) ///
    (bar meanrealsuccess treatment if treatment==3, bcolor("dkorange") fcolor("dkorange") lcolor(white)) ///
    (rcap hirealsuccess lorealsuccess treatment, lcolor(black) lwidth(medium)) ///
	(scatteri 0.479 2.1 0.486 2.1, recast(line) lcolor(black)) ///
	(scatteri 0.479 2.9 0.486 2.9, recast(line) lcolor(black)) ///
	(scatteri 0.486 2.1 0.486 2.9, recast(line) lcolor(black)) ///
	(scatteri 0.493 2.5 (0)"**", msymbol(none) lcolor(black)) ///
    (scatteri .5591 0.5 .5591 1.5, recast(line) lpattern(dash) lcolor(black)) ///
    (scatteri .6519 1.5 .6519 3.5, recast(line) lpattern(dash) lcolor(black)), ///
    ylabel(0(0.1)0.7, labsize(large) angle(0) nogrid) ///
    xlabel(1 "AON" 2 "sGMS" 3 "dGMS", labsize(large)) ///
    xtitle("Success", size(huge) margin(small) color(black)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(lcolor(white)) ///
    saving(c3, replace)
restore

bysort treatment session: egen realsurplus4=mean(surplus) if producer==1 & profitobjective==0
order realsurplus4
gsort treatment session  - period
sort treatment session realsurplus4
replace realsurplus4=. if realsurplus4[_n+1]==realsurplus4 & treatment[_n+1]==treatment & session[_n+1]==session

preserve
collapse (mean) meanrealsurplus= realsurplus4 (sd) sdrealsurplus=realsurplus4 (count) n=realsurplus4 if profitobjective==0, by(treatment)
generate hirealsurplus = meanrealsurplus + invttail(n-1,0.025)*(sdrealsurplus / sqrt(n))
generate lorealsurplus = meanrealsurplus - invttail(n-1,0.025)*(sdrealsurplus / sqrt(n))
sort treatment	 

twoway ///
    (bar meanrealsurplus treatment if treatment==1, bcolor(navy) fcolor(navy) lcolor(white)) ///
    (bar meanrealsurplus treatment if treatment==2, bcolor("orange_red") fcolor("orange_red") lcolor(white)) ///
    (bar meanrealsurplus treatment if treatment==3, bcolor("dkorange") fcolor("dkorange") lcolor(white)) ///
    (rcap hirealsurplus lorealsurplus treatment, lcolor(black) lwidth(medium)) ///
    (scatteri 48.5 1.1 49.0 1.1, recast(line) lcolor(black)) ///
    (scatteri 48.5 2.9 49.0 2.9, recast(line) lcolor(black)) ///
    (scatteri 49.0 1.1 49.0 2.9, recast(line) lcolor(black)) ///
    (scatteri 49.5 2 (0)"**", msymbol(none) lcolor(black)) ///
    (scatteri 38.39 2.1 38.89 2.1, recast(line) lcolor(black)) ///
    (scatteri 38.39 2.9 38.89 2.9, recast(line) lcolor(black)) ///
    (scatteri 38.89 2.1 38.89 2.9, recast(line) lcolor(black)) ///
    (scatteri 39.39 2.5 (0)"**", msymbol(none) lcolor(black)) ///
    (scatteri 33.8395 0.5 33.8395 1.5, recast(line) lpattern(dash) lcolor(black)) ///
    (scatteri 46.7272 1.5 46.7272 3.5, recast(line) lpattern(dash) lcolor(black)), ///
    ylabel(0(10)50, labsize(large) angle(0) nogrid) ///
    xlabel(1 "AON" 2 "sGMS" 3 "dGMS", labsize(large)) ///
    xtitle("Surplus", size(huge) margin(small) color(black)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(lcolor(white)) ///
    saving(c4, replace)
restore

graph combine "c3" "c4", xsize(5.5) ysize(3) graphregion(color(white))
drop realsuccess2 realsurplus4


//Pooled Objectives Table All Rounds

//Table
mean profit success surplus if treatment==1
tobit profit ib1.treatment c.threshold c.reservationprice meanvaluation if producer==1, ll(0) vce(cluster idnr)
probit success ib1.treatment c.threshold c.reservationprice meanvaluation if producer==1, vce(cluster idnr)
margins, dydx(*)
tobit surplus ib1.treatment c.threshold c.reservationprice meanvaluation if producer==1, ll(0) vce(cluster idnr)

//p-values
tobit profit ib3.treatment c.threshold c.reservationprice meanvaluation if producer==1, ll(0) vce(cluster idnr)
* For 1.treatment:
scalar b1 = _b[1.treatment]
scalar se1 = _se[1.treatment]
scalar t1 = b1/se1
scalar p_twosided1 = 2 * ttail(e(df_r), abs(t1))
* For H₀: beta >= 0 vs H₁: beta < 0:
scalar p_one1 = cond(b1 < 0, p_twosided1/2, 1 - p_twosided1/2)
display "One-sided p-value for 1.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one1
* For 2.treatment:
scalar b2 = _b[2.treatment]
scalar se2 = _se[2.treatment]
scalar t2 = b2/se2
scalar p_twosided2 = 2 * ttail(e(df_r), abs(t2))
scalar p_one2 = cond(b2 < 0, p_twosided2/2, 1 - p_twosided2/2)
display "One-sided p-value for 2.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one2


probit success ib3.treatment c.threshold c.reservationprice meanvaluation if producer==1, vce(cluster idnr)
margins, dydx(*)
matrix list r(table)
scalar m1 = r(table)[1, colnumb(r(table), "1.treatment")]
scalar se1 = r(table)[2, colnumb(r(table), "1.treatment")]
scalar t1 = m1/se1
scalar p_one = cond(m1 > 0, normal(t1), 1 - normal(t1))
display "One-sided p-value for marginal effect of 1.treatment: " p_one
matrix list r(table)
scalar m2 = r(table)[1, colnumb(r(table), "2.treatment")]
scalar se2 = r(table)[2, colnumb(r(table), "2.treatment")]
scalar t2 = m2 / se2
scalar p_one2 = cond(m2 < 0, normal(t2), 1 - normal(t2))
display "One-sided p-value for marginal effect of 2.treatment: " p_one2


tobit surplus ib3.treatment c.threshold c.reservationprice meanvaluation if producer==1, ll(0) vce(cluster idnr)
* For 1.treatment:
scalar b1 = _b[1.treatment]
scalar se1 = _se[1.treatment]
scalar t1 = b1/se1
scalar p_twosided1 = 2 * ttail(e(df_r), abs(t1))
* For H₀: beta >= 0 vs H₁: beta < 0:
scalar p_one1 = cond(b1 < 0, p_twosided1/2, 1 - p_twosided1/2)
display "One-sided p-value for 1.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one1
* For 2.treatment:
scalar b2 = _b[2.treatment]
scalar se2 = _se[2.treatment]
scalar t2 = b2/se2
scalar p_twosided2 = 2 * ttail(e(df_r), abs(t2))
scalar p_one2 = cond(b2 < 0, p_twosided2/2, 1 - p_twosided2/2)
display "One-sided p-value for 2.treatment (test H₀: β>=0 vs. H₁: β<0): " p_one2


/// Bidding Behavior in AON, sGMS and dGMS over Time
gen third=1 if period<16
replace third=2 if period>15 & period<31
replace third=3 if period>30

mean bnhigher if treatment==1, over(third)
mean bninline if treatment==1, over(third)
mean bntoolow if treatment==1, over(third)

mean toohigh if treatment==2, over(third)
mean bestresponse if treatment==2, over(third)
mean toolow if treatment==2, over(third)

mean toohighj if treatment==3, over(third)
mean bestresponsej if treatment==3, over(third)
mean toolowj if treatment==3, over(third)


egen freqappnew = sum(1) if !missing(valdecision), by(treatment third costid valdecision)
egen freqdynappnew = sum(1) if !missing(valdecision), by(treatment third costid valdecision bestresponsej)

label define thirdlbl 1 "(Rounds 1-15)" 2 "(Rounds 16-30)" 3 "(Rounds 31-45)"
label values third thirdlbl

twoway ///
    (scatter decision valuation [w=freqappnew] if treatment==1, ///
        msymbol(O) mcolor(gs10) msize(tiny)) ///
    (scatter decision valuation [w=freqappnew] if treatment==1 & bninline==0, ///
        msymbol(O) mcolor(black) msize(tiny)), ///
    by(costid third, note("") legend(off) title("") cols(3) yrescale xrescale ///
        graphregion(color(white)) ///
        plotregion(color(white))) ///
    xlabel(, labsize(medium) nogrid) ///
    ylabel(0(5)20, angle(horizontal) labsize(medium) nogrid) ///
    yscale(range(0 20)) ///
    xtitle("Value", size(large)) ///
    ytitle("Bid", size(large)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    xsize(6.5) ysize(10)


twoway ///
    (scatter decision valuation [w=freqappnew] if treatment==2, ///
        msymbol(O) mcolor(gs10) msize(tiny)) ///
    (scatter decision valuation [w=freqappnew] if treatment==2 & weaklydominated==1, ///
        msymbol(O) mcolor(black) msize(tiny)), ///
    by(costid third, note("") legend(off) title("") cols(3) yrescale xrescale ///
        graphregion(color(white)) ///
        plotregion(color(white))) ///
    xlabel(, labsize(medium) nogrid) ///
    ylabel(0(5)30, angle(horizontal) labsize(medium) nogrid) ///
    yscale(range(0 30)) ///
    xtitle("Value", size(large)) ///
    ytitle("Bid", size(large)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    xsize(6.5) ysize(10)
	
twoway ///
    (scatter decision valuation [w=freqappnew] if treatment==3, ///
        msymbol(O) mcolor(gs10) msize(tiny)) ///
    (scatter decision valuation [w=freqdynappnew] if treatment==3 & bestresponsej==0, ///
        msymbol(O) mcolor(black) msize(tiny)), ///
    by(costid third, note("") legend(off) title("") cols(3) yrescale xrescale ///
        graphregion(color(white)) ///
        plotregion(color(white))) ///
    xlabel(, labsize(medium) nogrid) ///
    ylabel(0(5)20, angle(horizontal) labsize(medium) nogrid) ///
    yscale(range(0 20)) ///
    xtitle("Value", size(large)) ///
    ytitle("Bid", size(large)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    xsize(6.5) ysize(10)


//// Share of Underbidding over Time in sGMS
 bysort idnr: egen avgtoolow=mean(toolow) if third>1
 bysort idnr: egen avgtoolowthird1=mean(toolow) if third==1
 bysort idnr: egen avgtoolowthird2=mean(toolow) if third==2
 bysort idnr: egen avgtoolowthird3=mean(toolow) if third==3
 sort idnr period
 replace avgtoolowthird1=avgtoolowthird1[_n-1] if avgtoolowthird1[_n-1]!=. & idnr==idnr[_n-1]
 replace avgtoolowthird2=avgtoolowthird2[_n-1] if avgtoolowthird2[_n-1]!=. & idnr==idnr[_n-1]
 replace avgtoolowthird3=avgtoolowthird3[_n-1] if avgtoolowthird3[_n-1]!=. & idnr==idnr[_n-1]
  gen hh7=100*avgtoolow+avgtoolowthird1 if period==45
 egen freqtoolow = sum(1) if !missing(hh7) & third>1 & period==45, by(treatment hh7)


 twoway ///
    (scatter avgtoolow avgtoolowthird1 [w=freqtoolow] if treatment==2 & period==45, ///
        msymbol(O) mcolor(black) msize(medium)) ///
    (scatteri 0 0 1 1, recast(line) lpattern(dash) lcolor(gs8)), ///
    xlabel(, labsize(medium) nogrid) ///
    ylabel(, angle(horizontal) labsize(medium) nogrid) ///
    xtitle("Rounds 1 to 15", size(large)) ///
    ytitle("Rounds 16 to 45", size(large)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white))


//// Distribution of Possibly Weakly Dominant Bids
bysort idnr: egen avgbestresponsej=mean(bestresponsej) if third>1
cdfplot avgbestresponse if treatment==2 & period==45
cdfplot avgbestresponsej if (treatment==2 | treatment==3) & period==45, by(treatment)

 
cdfplot avgbestresponsej if (treatment==2 | treatment==3) & period==45, by(treatment) ///
    legend(order(1 "Dynamized sGMS" 2 "dGMS") ///
        rows(1) position(12) ring(0) ///
        region(lcolor(white))) ///
    xlabel(, labsize(medium) nogrid) ///
    ylabel(, labsize(medium) nogrid) ///
    xtitle("Possibly weakly dominant bids", size(large)) ///
    ytitle("Cumulative Frequency", size(large)) ///
    graphregion(color(white)) ///
    plotregion(color(white))

order treatment session period idnr valuation decision earnings costs success profit surplus obtained payment
sort treatment session period idnr